package cn.pugle.jodatime;

import java.time.*;
import java.time.temporal.*;
import java.util.Date;

/**
 * Created by tzp on 2016/3/15.<br/>
 * trial from java.time package
 */
public class NewTime {
    public static void main(String[] args) {
        LocalDate dateNow = LocalDate.now();
        System.out.println("Now " + dateNow);
        LocalDate dateC = dateNow.withDayOfMonth(17);
        System.out.println("17day of month " + dateC);
        LocalDate dateA = LocalDate.ofYearDay(2015, 300);
        System.out.println("300day of 2015 " + dateA);
        /*Temporal接口，所有时间类的顶层接口。可使用TemporalAdjuster, TemporalAccessor, TemporalField, TemporalQuery 等对时间对象访问 调整*/
        LocalDate dateB = dateNow.with(new TemporalAdjuster() {
            @Override
            public Temporal adjustInto(Temporal temporal) {
                return temporal.with(ChronoField.DAY_OF_YEAR, 1);
            }
        });
        System.out.println("1month of year " + dateB);
        System.out.println(dateA.compareTo(dateB));
        System.out.println("----------- end date");

        LocalTime timeNow = LocalTime.now();
        System.out.println(timeNow);
        LocalTime time = timeNow.truncatedTo(ChronoUnit.SECONDS);
        System.out.println(time);
        System.out.println("----------- end time");

        System.out.println(LocalDateTime.ofEpochSecond(2329372709L,638337, ZoneOffset.ofHours(8)));
        System.out.println(new Date(2329372709000L));

        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("UTC"));
        System.out.println(ldt);
        System.out.println(ldt.query(TemporalQueries.zoneId()));//null

        ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.systemDefault());
        System.out.println(zonedDateTime);
        System.out.println(zonedDateTime.getZone().getRules());

        ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of(ZoneId.SHORT_IDS.get("CTT")));
        System.out.println(zdt);
        ZonedDateTime zdt1 = ZonedDateTime.now(ZoneId.ofOffset("UTC", ZoneOffset.of("+02:00")));
        System.out.println(zdt1);
        System.out.println("----------- end Zone");

        System.out.println("----------- end Periods");
        //不能用来求daysBetween;可以求出有几天。
        Duration days = Duration.between(LocalDateTime.of(2016,1,1,0,0),LocalDateTime.of(2016,3,15,0,0));
        System.out.println(days.toDays());
        System.out.println("----------- end Durations");
        long between = ChronoUnit.DAYS.between(LocalDateTime.of(2016, 1, 1, 0, 0), LocalDateTime.of(2016, 3, 15, 0, 0));
        System.out.println(between);


//        DateTimeFormatter dateTimeFormatter = DateTimeFormatterBuilder.getLocalizedDateTimePattern().
    }
}
